/**
 * 
 */
package com.revenco.bi.util;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.revenco.bi.model.userbean.AnychartDataBean;

/**
 * @author LingJun
 * @version $Revision:1.0.0, 2014年1月23日 下午2:59:17
 */
public class XmlUtils {
	/**
	 * 创建xml格式data字符串
	 * 
	 * @param teMap
	 *            其中Key 是 Legend 字段名字，Value 是统计或者计算后的一组数据
	 * @param seriesType
	 * @param seriesName
	 * @return
	 */
	public String createXML(List<AnychartDataBean> anychartDataBeanList,String seriesType, 
					String seriesName, String decimalDigits, String dataUnit) {
		StringBuilder seriesData = new StringBuilder();
		seriesData.append("<series name=\"" + seriesName + "\" " + " type=\""
				+ seriesType + "\" >");
		seriesData.append("<attributes>");
		seriesData.append("<attribute name=\"duration\"><![CDATA[" + dataUnit + "]]></attribute> ");
		seriesData.append("</attributes>");
		seriesData.append("<tooltip enabled=\"true\"> ");
		if("pie".equalsIgnoreCase(seriesType)) {
			seriesData.append("<format>"
					+ "{%Name} - {%SeriesName} : {%YValue}{numDecimals:"+decimalDigits+"}  "
							+ " Percent: {%YPercentOfSeries}{numDecimals: 2}%"
							+ "</format>");
		} else {
			seriesData.append("<format>{%Name} - {%SeriesName} : {%YValue}{numDecimals:"+decimalDigits+"}</format>");
		}
		seriesData.append("<position anchor=\"Center\" />");
		seriesData.append("</tooltip>");
		seriesData.append("<label enabled=\"false\" >");    //针对x轴数据过多情况，暂屏蔽数据显示，支持鼠标点击提示
		seriesData.append("<format>{%Value}{numDecimals:"+decimalDigits+"}</format>");
		seriesData.append("<position anchor=\"Center\" />");
		seriesData.append("</label>");
		for (int i = 0; i < anychartDataBeanList.size(); i++) {
			AnychartDataBean anychartDataBean = anychartDataBeanList.get(i);
			String key = anychartDataBean.getName();
			String val = anychartDataBean.getSumValue();
			String id = anychartDataBean.getId();
			seriesData
					.append("<point name=\"" + key + "\" y=\"" + val + "\" >");
			seriesData.append("<actions>");
			seriesData
					.append("<action type=\"call\" function=\"drillFunName\">");
			seriesData.append("<arg>"+ id+"</arg>");
			seriesData.append("<arg>"+val+"</arg>");
			seriesData.append("</action>");
			seriesData.append("</actions>");
			seriesData.append("</point>");
			seriesData.append("<attributes>");
			seriesData.append("<attribute  name=\"columnId\" > " + id
					+ " </attribute> ");
			seriesData.append("</attributes>");
		}
		seriesData.append("</series>");

		return seriesData.toString();
	}

	public String createXMLTest(Map<String, String> teMap, String seriesType,
			String seriesName) {
		StringBuilder seriesData = new StringBuilder();
		seriesData.append("<series name=\"" + seriesName + "\" " + " type=\""
				+ seriesType + "\" >");
		Iterator<Entry<String, String>> it = teMap.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, String> e = (Map.Entry<String, String>) it.next();
			Object key = e.getKey();
			Object val = e.getValue();
			seriesData.append("<point name=\"" + key + "\" y=\"" + val + "\">");
			seriesData.append("<actions>");
			seriesData
					.append("<action type=\"call\" function=\"pictureAnalyz.drillFunName\">");
			seriesData.append("<arg>A</arg>");
			seriesData.append("<arg>1</arg>");
			seriesData.append("</action>");
			seriesData.append("</actions>");
			seriesData.append("</point>");
		}
		seriesData.append("</series>");

		return seriesData.toString();
	}
	/*
	 * <actions> <action type="call" function="drillFunName"> <arg>A</arg>
	 * <arg>1</arg> </action> </actions>
	 */
	
	
	/**
	 * 拼装油量表pointer节点字符串
	 * @param teMap
	 * @param cName
	 * @param size
	 * @param color
	 * @return
	 */
	public String createXML2(Map<String, String> teMap, String cName, int size, String color, 
						String decimalDigits, String dataUnit) {
		StringBuilder seriesData = new StringBuilder();
		Iterator<Entry<String, String>> it = teMap.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, String> e = (Map.Entry<String, String>) it.next();
//			Object key = e.getKey();
			Object val = e.getValue();
			seriesData.append("<pointer name=\"" + cName + "\"  type=\"Needle\"  value=\"" + val
					+ "\" color=\""+color+"\" >");
			seriesData.append("<tooltip enabled=\"true\"> ");
			seriesData.append("<format>{%Value}{numDecimals:"+ decimalDigits+"}  " + dataUnit + "</format>");  
			seriesData.append("<position anchor=\"Center\" />");
			seriesData.append("</tooltip>");
			seriesData.append("<label enabled=\"true\" under_pointers=\"true\"> "
							+ " <format>" + cName + "</format> "
							+ "<position placement_mode=\"ByPoint\" x=\"0\" y=\"" + size + "\" valign=\"Center\" halign=\"Center\"/>"
							+ "<font color=\"" + color + "\" bold=\"false\" size=\"11\" family=\"Arial\"> "
							+ "</font>"	
							+ "<background enabled=\"false\">"
							+ "<fill type=\"Gradient\">"
							+ "<gradient>"
							+ "<key color=\"Rgb(247,247,247)\"/>"
							+"</gradient>"
							+"</fill>"
							+"<corners type=\"Rounded\" all=\"3\"/>"
							+"<border enabled=\"false\"/>"
							+"</background>"
							+"</label>");
			seriesData.append("</pointer>");
		}

		return seriesData.toString();
	}

}
